#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D u_Sampler;
uniform sampler2D u_Sampler1;
uniform sampler2D u_Sampler2;

uniform float isMobileF;

uniform vec4 dataRange;
uniform float u_Scale_lon;
uniform float u_Scale_lat;
uniform float u_Startlon;
uniform float u_Startlat;

uniform float pic_width;
uniform float pic_height;

uniform float leftlon;
uniform float lonspan;
uniform vec2 widhei;

uniform float _Alpha;
uniform float minValue;
uniform float maxValue;
uniform vec2 numRange;

void main(){
    float lon=leftlon+lonspan*(gl_FragCoord.x/widhei.x);
    float trueLon=lon;
    lon=mod(lon,360.);
    if(lon<0.){lon=lon+360.;}

    vec2 texturecoor=vec2(0.,gl_FragCoord.y/widhei.y);
    vec4 color1=texture2D(u_Sampler1,texturecoor);

    float lat=color1.x*255.+color1.y*2.55+color1.z*.0255;
    if(color1.w>0.){lat=lat*-1.;}

    vec2 v_Lonlat=vec2(lon,lat);
    float col_left=float(floor((lon-u_Startlon)/u_Scale_lon));
    float col_right=col_left+1.;
    float row_top=float(floor((u_Startlat-lat)/u_Scale_lat));
    float row_bottom=row_top+1.;

    float left_p=col_left/pic_width;
    float right_p=col_right/pic_width;
    float top_p=1.-(row_top/pic_height);
    float bottom_p=1.-(row_bottom/pic_height);

    vec4 color_lt=texture2D(u_Sampler,vec2(left_p,top_p));
    vec4 color_rt=texture2D(u_Sampler,vec2(right_p,top_p));
    vec4 color_lb=texture2D(u_Sampler,vec2(left_p,bottom_p));
    vec4 color_rb=texture2D(u_Sampler,vec2(right_p,bottom_p));

    float val_lt=color_lt.x*255.+color_lt.y/100.*255.;
    float val_rt=color_rt.x*255.+color_rt.y/100.*255.;
    float val_lb=color_lb.x*255.+color_lb.y/100.*255.;
    float val_rb=color_rb.x*255.+color_rb.y/100.*255.;

    vec4 necolor=vec4(0.,0.,0.,0.);
    float true_value=0.;
    float val_lt_check = color_lt.w*255.;
    float val_rt_check = color_rt.w*255.;
    float val_lb_check = color_lb.w*255.;
    float val_rb_check = color_rb.w*255.;
    if(val_lt_check<=0.||val_rt_check<=0.||val_lb_check<=0.||val_rb_check<=0.){
        val_lt = 255.0;
    }
    //不满足双线性 不插值
    if((val_lt<=numRange.x||val_lt>=numRange.y)||(val_rt<=numRange.x||val_rt>=numRange.y)||(val_lb<=numRange.x||val_lb>=numRange.y)||(val_rb<=numRange.x||val_rb>=numRange.y)){
        true_value=val_lt;
    }
    //计算双线性插值
    else{
        vec2 left_top=vec2(u_Startlon+u_Scale_lon*col_left,u_Startlat-u_Scale_lat*row_top);
        vec2 right_top=vec2(u_Startlon+u_Scale_lon*col_right,u_Startlat-u_Scale_lat*row_top);
        vec2 left_bottom=vec2(u_Startlon+u_Scale_lon*col_left,u_Startlat-u_Scale_lat*row_bottom);
        vec2 right_bottom=vec2(u_Startlon+u_Scale_lon*col_right,u_Startlat-u_Scale_lat*row_bottom);
        float rbxlbx=right_bottom.x-left_bottom.x;
        float rbxvx=right_bottom.x-v_Lonlat.x;
        if(rbxvx<=0.){
            rbxvx=0.;
        }
        if(rbxvx>=rbxlbx){
            rbxvx=rbxlbx;
        }
        float vxlbx=0.;
        vxlbx=rbxlbx-rbxvx;
        float r_1=rbxvx/rbxlbx*val_lb+vxlbx/rbxlbx*val_rb;
        float r_2=rbxvx/rbxlbx*val_lt+vxlbx/rbxlbx*val_rt;
        float ltylby=left_top.y-left_bottom.y;
        float ltyvy=left_top.y-v_Lonlat.y;
        if(ltyvy<=0.){
            ltyvy=0.;
        }
        if(ltyvy>=ltylby){
            ltyvy=ltylby;
        }
        float vylby=0.;
        vylby=ltylby-ltyvy;
        true_value=ltyvy/ltylby*r_1+vylby/ltylby*r_2;
    }
    //移动端 不插值
    if(isMobileF==1.){
        true_value=val_lt;
    }
    // if(true_value>=maxValue){
    //     true_value = (maxValue-minValue)/2.0;
    // }
    // if(true_value<=minValue){
    //     true_value = (maxValue-minValue)/2.0;
    // }
    if(true_value<=minValue){
        true_value = val_lt;
    }
    // if(true_value==minValue){
    //     true_value = val_lt;
    // }
    // true_value==minValue
    necolor=texture2D(u_Sampler2,vec2(0.,1.-(true_value-minValue)/(maxValue-minValue)/1.0));
    necolor.w=_Alpha;
    // if(true_value<minValue){
    //     necolor = vec4(255.,0.,0.,0.);
    // }
    // if(true_value==minValue){
    //     necolor = vec4(1.,0.,0.,0.);
    // }
    //超出数据范围 透明
    if(lon<dataRange.z||lon>dataRange.w||lat<dataRange.y||lat>dataRange.x){
        necolor=vec4(0.,0.,0.,0.);
    }
    //超出数值范围 透明
    if(true_value<numRange.x||true_value>numRange.y){
        necolor=vec4(0.,0.,0.,0.);
    }
    gl_FragColor=necolor;
}
